package com.zx.tradeclient.ctrl.index2;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index5.LoginActivity;

public class BuyFromSupplyActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private TextView buy_from_supply_name;
	private TextView buy_from_supply_company;
	private TextView buy_from_supply_pay;

	private EditText buy_from_supply_price;
	private EditText buy_from_supply_number;
	private EditText buy_from_supply_phone;
	private EditText buy_from_supply_contact;

	private Button buy_from_supply_button;

	private String demandId = "";
	private String productName = "";
	private String companyName = "";

	private ProgressDialog progressDialog;

	private TradeApp app;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buy_from_supply_info);

		if (!(ActivityTaskManager.getInstance().containsName("BuyFromSupplyActivity"))) {
			ActivityTaskManager.getInstance().putActivity("BuyFromSupplyActivity", this);
		}

		init();

	}

	private void init() {

		setHeader(0);

		app = (TradeApp) getMyApplication();

		demandId = getIntent().getStringExtra("demandId");
		productName = getIntent().getStringExtra("productName");
		companyName = getIntent().getStringExtra("companyName");

		buy_from_supply_name = (TextView) findViewById(R.id.buy_from_supply_name);
		buy_from_supply_name.setText(productName);

		buy_from_supply_company = (TextView) findViewById(R.id.buy_from_supply_company);
		buy_from_supply_company.setText(companyName);

		buy_from_supply_pay = (TextView) findViewById(R.id.buy_from_supply_pay);
		buy_from_supply_price = (EditText) findViewById(R.id.buy_from_supply_price);
		buy_from_supply_number = (EditText) findViewById(R.id.buy_from_supply_number);
		buy_from_supply_phone = (EditText) findViewById(R.id.buy_from_supply_phone);
		buy_from_supply_contact = (EditText) findViewById(R.id.buy_from_supply_contact);

		buy_from_supply_pay.setText("线下交易");

		buy_from_supply_button = (Button) findViewById(R.id.buy_from_supply_button);
		buy_from_supply_button.setOnClickListener(this);
	}

	public void postSupplyOrderInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "demandOrderForm");
		map.put("demandId", demandId);

		map.put("userId", app.user.getId());
		map.put("price", buy_from_supply_price.getText().toString());
		map.put("num", buy_from_supply_number.getText().toString());
		map.put("linkMan", buy_from_supply_contact.getText().toString());
		map.put("linkMobile", buy_from_supply_phone.getText().toString());
		map.put("tradeWay", "402881e93be49a32013be4b8d2410002");

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);
			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(BuyFromSupplyActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			}
		});

	}

	private void parseJson(JSONObject response) {
		try {
			String succeed = response.getString("succeed");

			if (succeed.equals("000")) {
				Toast toast2 = Toast.makeText(BuyFromSupplyActivity.this, "提交供货信息成功！", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			} else {
				Toast toast2 = Toast.makeText(BuyFromSupplyActivity.this, "无数据", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			}

			progressDialog.dismiss();

		} catch (JSONException e) {
			progressDialog.dismiss();
			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		} else if (v == buy_from_supply_button) {

			if (app.user.getId() == "") {
				Intent intent = new Intent(BuyFromSupplyActivity.this, LoginActivity.class);
				startActivity(intent);
			} else {
				progressDialog = ProgressDialog.show(BuyFromSupplyActivity.this, null, "正在获取数据，请稍后.....", true, true);

				postSupplyOrderInfo();
			}

		}

	}

}